package com.ymm.platform.core.facade;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ymm.platform.api.constants.dto.LltorderDetailsDTO;
import com.ymm.platform.api.constants.dto.OrderShowDTO;
import com.ymm.platform.api.constants.dto.QueryShipperDTO;
import com.ymm.platform.api.constants.dto.ShipperShowDTO;
import com.ymm.platform.api.constants.entity.CargoEntity;
import com.ymm.platform.api.constants.entity.DriverEntity;
import com.ymm.platform.api.constants.entity.ShipperEntity;
import com.ymm.platform.api.facade.PlatformQueryShipperService;
import com.ymm.platform.api.utils.Result;
import com.ymm.platform.dao.mapper.QueryDriverMapper;
import com.ymm.platform.api.utils.JudgePagination;
import com.ymm.platform.dao.mapper.QueryShipperMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @Description: 货主查询服务实现
 * @Author: Xuecheng Li
 * @CreateTime: 2022-08-03 20:46
 * @Version: 1.0.0
 */
@Slf4j
@Component
@Service
public class PlatformQueryShipperServiceImpl implements PlatformQueryShipperService {

    @Resource
    private QueryShipperMapper queryShipperMapper;
    private JudgePagination judgePagination=new JudgePagination();

    @Override
    public Result<PageInfo> queryAllShipper(Integer pageNum,Integer pageSize) {
        judgePagination.judgePaginationPlausibility(pageNum,pageSize);
        PageHelper.startPage(pageNum,pageSize);
        List<ShipperEntity> shipperEntitiesList = queryShipperMapper.queryAllShipper();
        PageInfo<ShipperEntity> pageInfo=new PageInfo(shipperEntitiesList);
        return Result.success(pageInfo);

    }

    @Override
    public Result<PageInfo> queryShipperOnCondition(Integer pageNum,Integer pageSize, QueryShipperDTO queryShipperDTO) {
        judgePagination.judgePaginationPlausibility(pageNum,pageSize);
        PageHelper.startPage(pageNum, pageSize);
        List<ShipperEntity> driverEntityList = queryShipperMapper.queryShipperOnCondition(queryShipperDTO);
        PageInfo<ShipperEntity> pageInfo=new PageInfo(driverEntityList);
        return Result.success(pageInfo);


    }

    @Override
    public Result<Long> queryShipperCompleteOrder(Long shipperId) {
        return Result.success(queryShipperMapper.queryShipperCompleteOrder(shipperId));
    }

    @Override
    public Result<PageInfo> queryShipperCompleteOrderList(Integer pageNum,Integer pageSize, Long shipperId) {
        judgePagination.judgePaginationPlausibility(pageNum,pageSize);
        List<OrderShowDTO> orderShowDTOS = new ArrayList<>();
        PageHelper.startPage(pageNum, pageSize);
        List<LltorderDetailsDTO> listLltorderDetails = queryShipperMapper.queryShipperCompleteOrderList(shipperId);
        for(LltorderDetailsDTO lltorderDetailsDTOs:listLltorderDetails){
            orderShowDTOS.add(orderDetailsToShow(lltorderDetailsDTOs));
        }
        PageInfo<OrderShowDTO> pageInfo=new PageInfo(orderShowDTOS);
        return Result.success(pageInfo);

    }

    @Override
    public Result<Long> queryShipperUncompleteOrder(Long shipperId) {
        return Result.success(queryShipperMapper.queryShipperUncompleteOrder(shipperId));
    }

    @Override
    public Result<PageInfo> queryShipperUncompleteOrderList(Integer pageNum,Integer pageSize, Long shipperId) {
        judgePagination.judgePaginationPlausibility(pageNum,pageSize);
        List<OrderShowDTO> orderShowDTOS = new ArrayList<>();
        PageHelper.startPage(pageNum, pageSize);
        List<LltorderDetailsDTO> listLltorderDetails = queryShipperMapper.queryShipperUncompleteOrderList(shipperId);
        for(LltorderDetailsDTO lltorderDetailsDTOs:listLltorderDetails){
            orderShowDTOS.add(orderDetailsToShow(lltorderDetailsDTOs));
        }
        PageInfo<OrderShowDTO> pageInfo=new PageInfo(orderShowDTOS);
        return Result.success(pageInfo);

    }

    @Override
    public Result<Integer> queryShipperTotalOutcome(Long shipperId) {
        List<LltorderDetailsDTO> lltorderDetailsDTOS =queryShipperMapper.queryShipperCompleteOrderList(shipperId);
        Integer s=0;
        for(LltorderDetailsDTO lltorderDetailsDTO:lltorderDetailsDTOS){
            s=s+lltorderDetailsDTO.getOrderPrice();
        }
        return Result.success(s);
    }

    @Override
    public Result<Long> queryShipperTotalShipments(Long shipperId) {

        return Result.success(queryShipperMapper.queryShipperTotalShipments(shipperId));
    }

    @Override
    public Result<PageInfo> queryShipperPublishCargo(Integer pageNum,Integer pageSize,Long shipperId) {
        judgePagination.judgePaginationPlausibility(pageNum,pageSize);

        PageHelper.startPage(pageNum, pageSize);
        List<CargoEntity> cargoEntities = queryShipperMapper.queryShipperPublishCargo(shipperId);

        PageInfo<OrderShowDTO> pageInfo=new PageInfo(cargoEntities);
        return Result.success(pageInfo);

    }


    private OrderShowDTO orderDetailsToShow(LltorderDetailsDTO lltorderDetailsDTO){
        OrderShowDTO orderShowDTO = new OrderShowDTO();
        //orderShowDTO.setTradeId(lltorderDetailsDTO.getTradeId());
        orderShowDTO.setOrderId(lltorderDetailsDTO.getOrderId());
        orderShowDTO.setTradeDriverState(lltorderDetailsDTO.getTradeEntity().getTradeDriverState());
        orderShowDTO.setOrderShipperState(lltorderDetailsDTO.getOrderShipperState());
        orderShowDTO.setShipperName(lltorderDetailsDTO.getShipperEntity().getNickname());
        orderShowDTO.setShipperPhone(lltorderDetailsDTO.getShipperEntity().getPhoneNumber());
        orderShowDTO.setDriverName(lltorderDetailsDTO.getDriverEntity().getDriverName());
        orderShowDTO.setDriverPhone(lltorderDetailsDTO.getDriverEntity().getDriverPhone());
        orderShowDTO.setDriverType(lltorderDetailsDTO.getDriverEntity().getDriverType());
        orderShowDTO.setDriverCarNum(lltorderDetailsDTO.getDriverEntity().getDriverCarNum());
        orderShowDTO.setOrderCreateDate(lltorderDetailsDTO.getOrderCreateDate());
        orderShowDTO.setTradeArriveTime(lltorderDetailsDTO.getTradeEntity().getTradeArriveTime());
        orderShowDTO.setOrderPrice(lltorderDetailsDTO.getOrderPrice());
        orderShowDTO.setCargoSrc(lltorderDetailsDTO.getCargoEntity().getCargoSrc());
        orderShowDTO.setCargoDes(lltorderDetailsDTO.getCargoEntity().getCargoDes());
        orderShowDTO.setCargoTip(lltorderDetailsDTO.getCargoEntity().getCargoComment());
        orderShowDTO.setDriverCarType(lltorderDetailsDTO.getDriverEntity().getDriverCarType());

        return orderShowDTO;
    }

    @Override
    public Result<PageInfo> queryShipperListByName(Integer pageNum, Integer pageSize, String shipperName) {
        judgePagination.judgePaginationPlausibility(pageNum,pageSize);
        PageHelper.startPage(pageNum, pageSize);
        List<ShipperEntity> shipperEntities = queryShipperMapper.queryShipperOnName(shipperName);

        PageInfo<ShipperEntity> pageInfo=new PageInfo(shipperEntities);
        return Result.success(pageInfo);
    }

    @Override
    public Result<PageInfo> queryShipperListByPhone(Integer pageNum, Integer pageSize, String shipperPhoneNum) {
        judgePagination.judgePaginationPlausibility(pageNum,pageSize);
        PageHelper.startPage(pageNum, pageSize);
        List<ShipperEntity> shipperEntities = queryShipperMapper.queryShipperOnPhone(shipperPhoneNum);

        PageInfo<ShipperEntity> pageInfo=new PageInfo(shipperEntities);
        return Result.success(pageInfo);
    }

    @Override
    public Result<PageInfo> sortShipperByVipTypeD(Integer pageNum, Integer pageSize, Integer desc) {
        judgePagination.judgePaginationPlausibility(pageNum,pageSize);
        PageHelper.startPage(pageNum, pageSize);
        List<ShipperEntity> shipperEntities = queryShipperMapper.sortShipperByVipTypeD();

        PageInfo<ShipperEntity> pageInfo=new PageInfo(shipperEntities);
        return Result.success(pageInfo);
    }

    @Override
    public Result<PageInfo> sortShipperByVipTypeA(Integer pageNum, Integer pageSize, Integer asc) {
        judgePagination.judgePaginationPlausibility(pageNum,pageSize);
        PageHelper.startPage(pageNum, pageSize);
        List<ShipperEntity> shipperEntities = queryShipperMapper.sortShipperByVipTypeA();

        PageInfo<ShipperEntity> pageInfo=new PageInfo(shipperEntities);
        return Result.success(pageInfo);

    }



}
